﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace GameMassage
{
    public delegate void GameEventDelegate(object obj);

    public class GameEventHandle
    {

        public GameEventDelegate gameEventDelegate;

    }

    /// <summary>
    /// 游戏中模块间交互使用
    /// </summary>
    public class GameMessageCenter : NetUtil.Singleton<GameMessageCenter>
    {

        public Dictionary<MessageId, GameEventHandle> messageCenter = new Dictionary<MessageId, GameEventHandle>();

        /// <summary>
        /// 发送消息
        /// </summary>
        public void FireMessage(MessageId msgId, object obj = null)
        {

            //向注册消息的对象发送消息
            if (messageCenter.ContainsKey(msgId))
            {

                messageCenter[msgId].gameEventDelegate(obj);

            }

        }

        public void RegisterGameMsg(MessageId msgId, GameEventDelegate eventCallBack)
        {

            if (!messageCenter.ContainsKey(msgId))
            {

                GameEventHandle gameEventHandle = new GameEventHandle();

                messageCenter.Add(msgId, gameEventHandle);

            }

            messageCenter[msgId].gameEventDelegate += eventCallBack;

        }

        public void RemoveGameMsg(MessageId msgId, GameEventDelegate eventCallBack = null)
        {
            if (!messageCenter.ContainsKey(msgId))
            {
                if (eventCallBack != null)
                {
                    messageCenter.Remove(msgId);
                }
                else
                {
                    messageCenter[msgId].gameEventDelegate -= eventCallBack;
                }

            }

        }

    }

    /// <summary>
    /// 游戏中消息传递id
    /// </summary>
    public enum MessageId
    {

        /// <summary>
        /// 连接成功
        /// </summary>
        CONNECT_SUCCESS =0,

        /// <summary>
        /// 登录成功
        /// </summary>
        LOGIN_SUCCESS,

    }

}